﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;

#endregion


namespace ESF.General.Text
{
	public class ArgsReader
	{
		#region Miembros
		
		private Dictionary<string, string> _Args;
	
		#endregion
		
		
		#region Constructor
		
		private ArgsReader () {}
		
		
		public ArgsReader (string [] pArgs)
		{
			_Args = GetArgs (pArgs);
		}
		
		#endregion
		
		
		#region Propiedades
		
		public bool IsHelp
		{
			get { return _Args.ContainsKey ("?"); }
		}
		
		#endregion
		
		
		#region Funciones
		
		public bool ContainsAllKeys (params string [] pKeys)
		{
			if (pKeys == null || pKeys.Length == 0) { return false; }
		
			foreach (string key in pKeys)
			{
				if (!_Args.ContainsKey (key))
				{
					return false;
				}
			}
			
			return true;
		}
		
		#endregion
		
	
		#region Lectura de Bool
	
		public bool? GetArgBool (bool pRequired, string pKey)
		{
			return GetArgBool (pRequired, pKey, false);
		}
										
	
		public bool? GetArgBool (bool pRequired, string pKey, bool pDefaultValue)
		{
			return Validate.FilledBool (pRequired, pKey, GetArg (pRequired, pKey, pDefaultValue.ToString ()));
		}
	
		#endregion
		
		
		#region Lectura de Int32
	
		public int? GetArgInt32 (bool pRequired, string pKey)
		{
			return GetArgInt32 (pRequired, pKey, 0);
		}
										
	
		public int? GetArgInt32 (bool pRequired, string pKey, int pDefaultValue)
		{
			return Validate.FilledInt32 (pRequired, pKey, GetArg (pRequired, pKey, pDefaultValue.ToString ()));
		}
	
		#endregion
	
	
		#region Lectura Basica
	
		public string GetArg(bool pRequired, string pKey)
		{
			return GetArg (pRequired, pKey, null);					
		}
		
		
		public string GetArg(bool pRequired, string pKey, string pDefaultValue)
		{
			string tmpValue;
			
		
			if (_Args.ContainsKey(pKey))
			{
				tmpValue = Validate.Filled (pRequired, pKey, _Args[pKey]);
			}
			else
			{
				tmpValue = pDefaultValue;
			}

			Console.WriteLine ("Arg \"{0}\" Value: {1}", pKey, tmpValue);

			return tmpValue;
		}
		
		
		public Dictionary<string, string> GetArgs(string[] pArgs)
		{
			Dictionary<string, string> dicArgs;

			string tmpArgname;


			dicArgs = new Dictionary<string, string>();

			for (int i = 0; i < pArgs.Length; i++)
			{
				if (pArgs[i][0] == '-')
				{
					tmpArgname = pArgs[i].Substring(1);

					if (dicArgs.ContainsKey(tmpArgname))
					{
						throw new Exception(string.Format("The argument {0} is already defined", tmpArgname));
					}
					else
					{
						dicArgs.Add(tmpArgname, string.Empty);

						if (i + 1 < pArgs.Length)
						{
							dicArgs[tmpArgname] = pArgs[++i];
						}
					}
				}
			}

			return dicArgs;
		}
		
		#endregion
		
		
		#region Presentacion
		
		public void ShowArgs()
		{
			Console.Write("Parameters:");

			foreach (KeyValuePair<string, string> arg in _Args)
			{
				Console.WriteLine("{0}: {1}", arg.Key, arg.Value);
			}
		}
		
		#endregion
	}
}
